package com.imclient.server;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.UUID;

import com.imclient.headler.AbstractHeadler;
import com.imclient.headler.LoginHeadler;
import com.imclient.message.Header;
import com.imclient.message.Message;
import com.imclient.type.ConnInfoType;
import com.imclient.type.HeaderType;
import com.imclient.type.PushType;

public abstract class MessageClient {
	
	private  String username;
	
	private String password;
	
	private String host;
	
	private int port;
	
	private HeadlerServer hs;
	
	private long reConnectTime = 0L;
	
	
	
	
	public void setReConnectTime(long reConnectTime) {
		this.reConnectTime = reConnectTime;
	}

	private long keeplive = 60000;
	
	private ArrayList<AbstractHeadler>  hlist = new ArrayList<AbstractHeadler>();
	
	public void addHeadler(AbstractHeadler ah){
		hlist.add(ah);
	}
	
	public MessageClient setUserInfo(String username,String password){
		
		this.username = username;
		this.password = password;
		return MessageClient.this;
		
	}
	
	public void close(){
		if(hs!=null){
			hs.setMclose(true);
			hs.close();
		}
	}
	
	public MessageClient setNetInfo(String host,int port){
		this.host = host;
		this.port = port;
		return MessageClient.this;
	}
	
	public String sengMsgByUserId(String userId,String msg){
		Message login = new Message();
		String id = getMessageId();
		Header[] headers = new Header[]{new Header(HeaderType.Type.name(),PushType.MESSAGE.name()),new Header(HeaderType.From.name(),username),new Header(HeaderType.To.name(),userId),new Header(HeaderType.Messageid.name(),id)};
		login.setHeaders(headers);
		try {
			login.setBody(msg.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
		if(sendMsg(login)){
			return id;
		}
		return null;
	}
	
//	private boolean isIP(String str){
//		int num = 0;
//		int i;
//		for (i = 0; i < str.length(); i++) {
//			if (str.charAt(i)== '.') {
//				num++;
//				continue;
//			}
//			if (str.charAt(i) < '0' || str.charAt(i) > '9') {
//				return false;
//			}
//		}
//		if (num != 3) {
//			return false;
//		}
//		return true;
//	}
	
	
	
	private String getMessageId(){
		return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 6);
	}
	
	public boolean sendMsg(Message msg){
		if(hs!=null){
			return hs.SendMsg(msg);
		}
		return false;
	}
	
	public abstract void connectInfo(ConnInfoType ct);
	
	
	
	
	public void setKeeplive(long keeplive) {
		this.keeplive = keeplive;
	}

	public void start() throws Exception{
		hs = new HeadlerServer(this.host, this.port){

			@Override
			public void connectInfo(ConnInfoType ct) {
				MessageClient.this.connectInfo(ct);
				
			}};
		LoginHeadler loginHeadler = new LoginHeadler(this.username, this.password);
		hs.setReConnectTime(this.reConnectTime);
		loginHeadler.setKeeplive(this.keeplive);
		hs.addHeadler(loginHeadler);
		if(hlist.size()>0){
			for(AbstractHeadler a:hlist){
				hs.addHeadler(a);
			}
		}
		hs.start();
	}

}
